<?php
/**
 * @name: UserService.class.php 简要描述本类的用途.
 * @package:
 * @author: Liuyi
 * @created: 2/17/17 2:41 PM
 * @copyright: Fiidee. Monda. Cn
 */

namespace api\service;

use api\service\interfaces\IUserService;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\core\Log;
use herosphp\utils\AjaxResult;
Loader::import('api.service.interfaces.IUserService');
class UserService implements IUserService {

    private $__service;
    public function __construct()
    {
        $this -> __service = Beans::get('user.user.service');
    }

    /**
     * 通过微信创建一个用户,如果存在就直接返回用户信息
     * @param $wx_id
     * @return mixed|void
     */
    public function createUserByWeiXin($wx_id)
    {
        if(!$wx_id)AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"error params!",null);

        //尝试获取用户信息，成功就直接返回
        $data = $this -> __service -> getItemByWeiXin($wx_id);
        if( $data ){
            return AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,"操作成功",$data);
        }
        if( $this -> __service -> addByWx($wx_id,2) ){
            $data = $this -> __service -> getItemByWeiXin($wx_id);
            return AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,"操作成功",$data);
        }else{
            $message = $this -> __service -> getErrorMsg();
            return AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,$message,"");
        }
    }

    /**
     * @param $username
     * @param $password
     * @return mixed|void
     */
    public function login($username,$password)
    {
        return true;
    }

    /**
     * 个人认证
     * @param $username
     * @param $password
     * @param $wx_id
     * @return mixed|void
     */
    public function authUser($username,$password,$wx_id){
        if(!$username)AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"error params!",null);
        if(!$password)AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"error params!",null);
        if(!$wx_id)AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"error params!",null);
        $res = $this -> __service -> authUser($username,md5($password),$wx_id);
        if( $res ){
            return AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,"操作成功",$res);
        }else{
            $message = $this -> __service -> getErrorMsg();
            return AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,$message,"");
        }
    }

    /**
     * 根据登陆的微信id获取用户信息
     * @param $wx_id
     */
    public function getUserByWeiXin( $wx_id ){
        if(!$wx_id)AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"error params!",null);
        $items = $this -> __service -> getItemByWeiXin( $wx_id );
        if( $items !== false ){
            return AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,"操作成功",$items);
        }else{
            Log::error("微信号{$wx_id}获取用户失败，没有该用户信息！");
            return AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"操作失败","");
        }
    }

    /**
     * @param $conditions
     */
    public function count( $conditions ){
        if(!is_array($conditions))AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"error params!",null);
        if( $this -> __service -> count($conditions) ){
            return AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS,"操作成功","");
        }else{
            return AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"操作失败","");
        }
    }
}